import { useRef, useEffect } from 'react';
import styles from './index.module.scss';

interface ITextArea
  extends React.DetailedHTMLProps<
    React.TextareaHTMLAttributes<HTMLTextAreaElement>,
    HTMLTextAreaElement
  > {
  value?: string;
}

export default function TextArea(props: ITextArea) {
  const { value = '', maxLength = 100, autoFocus = true, ...restProps } = props;

  const textRef = useRef<HTMLTextAreaElement>(null);

  useEffect(() => {
    textRef.current?.setSelectionRange(-1, -1);
  }, []);
  return (
    <div className={styles.root}>
      <textarea
        ref={textRef}
        autoFocus={autoFocus}
        className={'textarea'}
        value={value}
        maxLength={maxLength}
        {...restProps}
      />
      <div className="count">
        {value.length}/{maxLength}
      </div>
    </div>
  );
}
